﻿using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.Materials.RecipientsToAppls;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.Materials.RecipientsToAppls
{
    public class RecipientsToApplsRepository : DapperDbContextRepository, IRecipientsToApplsRepository
    {
        public RecipientsToApplsRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 物资领用——查询分页显示
        /// <summary>
        /// 物资领用——查询分页显示
        /// </summary>
        /// <param name="Recipient"></param>
        /// <param name="ReceiveTimek"></param>
        /// <param name="ApprovalStatusk"></param>
        /// <param name="Approverk"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<RecipientsToApplsDto>> GetRecipientsToApplsPageShow(string Recipient, DateTime? ReceiveTimek, int ApprovalStatusk, string Approverk,  int pageindex = 1, int pagesize = 3)
        {
            PageResponse<RecipientsToApplsDto> response = new PageResponse<RecipientsToApplsDto>();
            var sql = "select a.*,b.DictionaryName as BName from RecipientsToAppl a join Dictionary b on a.ApprovalStatusk=b.Id where a.IsDelete=0";
            if (!string.IsNullOrWhiteSpace(Recipient))
            {
                sql += $" and a.Recipient like '%" + Recipient + "%'";
            }
            if (ReceiveTimek != null)
            {
                sql += $" and a.ReceiveTimek='{ReceiveTimek}'";
            }
            if (ApprovalStatusk > 0)
            {
                sql += $" and a.ApprovalStatusk='{ApprovalStatusk}'";
            }
            if (!string.IsNullOrWhiteSpace(Approverk))
            {
                sql += $" and a.Approverk like '%" + Approverk + "%'";
            }
            var list = await this.QueryAsync<RecipientsToApplsDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<RecipientsToApplsDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 物资领用——逻辑删除
        /// <summary>
        /// 物资领用——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteRecipientsToAppls(int id)
        {

            var sql = $"update RecipientsToAppl set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        #endregion

        #region 物资领用——批量删除
        /// <summary>
        /// 物资领用——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteBatchRecipientsToAppls(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update RecipientsToAppl set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        #endregion

        #region 物资领用——编辑
        /// <summary>
        /// 物资领用——编辑
        /// </summary>
        /// <param name="purchaseRequestDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateRecipientsToAppls(RecipientsToApplsDto recipientsToApplsDto)
        {
            var sql = $"update RecipientsToAppl set RecipientsToApplyNumber='{recipientsToApplsDto.RecipientsToApplyNumber}',Headlinek='{recipientsToApplsDto.Headlinek}',UseDescribes='{recipientsToApplsDto.UseDescribes}',Remarkm='{recipientsToApplsDto.Remarkm}',RecipientsItems='{recipientsToApplsDto.RecipientsItems}',Recipient='{recipientsToApplsDto.Recipient}',ReceiveTimek='{recipientsToApplsDto.ReceiveTimek}',EstablishTimek='{recipientsToApplsDto.EstablishTimek}',ApprovalStatusk='{recipientsToApplsDto.ApprovalStatusk}',Approverk='{recipientsToApplsDto.Approverk}',IsDelete='{recipientsToApplsDto.IsDelete}' where Id='{recipientsToApplsDto.Id}'";
            return await ExecuteAsync(sql, recipientsToApplsDto);
        }
        #endregion

        #region 物资领用——添加
        /// <summary>
        /// 物资领用——添加
        /// </summary>
        /// <param name="purchaseRequestDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddRecipientsToAppls(RecipientsToApplsDto recipientsToApplsDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into RecipientsToAppl values('{id}','{recipientsToApplsDto.Headlinek}','{recipientsToApplsDto.UseDescribes}','{recipientsToApplsDto.Remarkm}','{recipientsToApplsDto.RecipientsItems}','{recipientsToApplsDto.Recipient}',getDate(),getDate(),20,'{recipientsToApplsDto.Approverk}',0)";
            return await ExecuteAsync(sql, recipientsToApplsDto);
        }
        #endregion
    }
}
